package com.authine.cloudpivot.web.api.service.impl;

import com.authine.cloudpivot.web.api.dao.AtsMapper;
import com.authine.cloudpivot.web.api.dubbo.DubboConfigService;
import com.authine.cloudpivot.web.api.service.AtsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;


@Slf4j
@Service
public class AtsServiceImpl implements AtsService {

    @Autowired
    private AtsMapper atsMapper;
    @Autowired
    private DubboConfigService dubboConfigService;

    @Override
    public Map<String, String> getAtsMap(Map<String, String> map) {
        Map<String, String> returnMap = new HashMap<>();
        String city = atsMapper.getIdByAddress(map.get("city"));
        if(StringUtils.isNotEmpty(city)){
            returnMap.put("id",city);
            return returnMap;
        }
        String province = atsMapper.getIdByAddress(map.get("province").replaceAll("市","").replaceAll("省",""));
        if(StringUtils.isNotEmpty(province)){
            returnMap.put("id",province);
            return returnMap;
        }
        returnMap.put("id","1");
        return returnMap;
    }

    @Override
    public Integer updateDemandDetail(String demandDetailId, String idCode, String atsId) {
        return atsMapper.updateDemandDetail(demandDetailId,idCode,atsId);
    }

    @Override
    public List<String> getWeeklyReport(LocalDateTime now) {
        List<String> weeklyIds = atsMapper.getWeeklyReport(now);
        return weeklyIds;
    }

    @Override
    public Integer updateWeeklyReport(List<Map<String, Object>> updateData) {
        Integer count = 0;
        for (Map<String, Object> updateDatum : updateData) {
            atsMapper.updateWeeklyReport(updateDatum);
            count++;
        }
        return count;
    }

    @Override
    public Map<String, List<String>> validateSchemaCode(List<String> ids, String schemaCode) {
        List<Map<String, String>> codes = atsMapper.getCodeByRelativeCode(schemaCode);
        List<String> returnIds = new ArrayList<>();
        List<String> notReturnIds = new ArrayList<>();
        Set<String> useIds = new HashSet<>();
        for (Map<String, String> codeMap : codes) {
            String code = codeMap.get("code");
            String tableCode = codeMap.get("schemaCode");
            String tableName = getTableNameByCode(tableCode);
            String tableExist = atsMapper.getTableExist(tableName);
            if(StringUtils.isBlank(tableExist)){
                continue;
            }
            for (String id : ids) {
                Integer count = atsMapper.getTableCodeUse(tableName,code,id);
                if(count > 0){
                    useIds.add(id);
                }
            }
        }
        for (String id : ids) {
            if(!useIds.contains(id)){
                returnIds.add(id);
            }
        }
        notReturnIds.addAll(useIds);
        Map<String, List<String>> map = new HashMap<>();
        map.put("returnIds",returnIds);
        map.put("notReturnIds",notReturnIds);
        return map;
    }

    @Override
    public void closeDemandDetail(String id) {
        atsMapper.closeDemandDetail(id);
    }

    @Override
    public void addDemandDetail(String id, Integer count) {
        atsMapper.addDemandDetail(id, count);
    }

    @Override
    public void updateWeeklySumReport(List<Map<String, Object>> updateData) {
        List<String> updates = new ArrayList<>();
        for (Map<String, Object> updateDatum : updateData) {
            String id = atsMapper.getUpdateWeeklyReport(updateDatum);
            updates.add(id);
        }
        List<Map<String, Object>> updateWeeklyReportSum = atsMapper.getUpdateWeeklyReportSum(updates);
        for (Map<String, Object> map : updateWeeklyReportSum) {
            Map<String, Object> newMap = new HashMap<>();
            newMap.putAll(map);
            Integer demandFor = MapUtils.getInteger(map, "demandFor");
            Integer entryNum = MapUtils.getInteger(map, "entryNum");
            if(demandFor == null||demandFor == 0){
                newMap.put("finishingRate",0);
            }else if(entryNum == null||entryNum == 0){
                newMap.put("finishingRate",0);
            }else{
                BigDecimal decimalB = new BigDecimal(entryNum);
                BigDecimal demandForB = new BigDecimal(demandFor);
                newMap.put("finishingRate",decimalB.divide(demandForB,2,BigDecimal.ROUND_UP));
            }
        }
        for (Map<String, Object> map : updateWeeklyReportSum) {
            atsMapper.updateWeeklyReportSum(map);
        }
    }

    @Override
    public void saveWeeklyReportDetail(List<Map<String, Object>> list) {
        if(CollectionUtils.isNotEmpty(list)){
            List<Map<String, Object>> values = new ArrayList<>();
            for (Map<String, Object> map : list) {
                String id = UUID.randomUUID().toString().replaceAll("-","");
                Map<String, Object> value = new HashMap<>();
                value.putAll(map);
                value.put("id",id);
                value.put("createdTime",new Date());
                values.add(value);
            }
            atsMapper.saveWeeklyReportDetail(values);
        }
    }

    @Override
    public Map<String, Integer> getFinishedById(String id) {
        return atsMapper.getFinishedById(id);
    }

    @Override
    public String getUserIdByName(String name) {
        return atsMapper.getUserIdByName(name);
    }

    @Override
    public Integer findStartCount(String parentId) {
        return atsMapper.findStartCount(parentId);
    }

    @Override
    public void closeRecruitmentDemand(String parentId) {
        atsMapper.closeRecruitmentDemand(parentId);
    }

    @Override
    public void updateAll(LocalDateTime parse) {
        atsMapper.updateAll(parse);
    }

    private String getTableNameByCode(String code){
        return dubboConfigService.getBizObjectFacade().getTableName(code);
    }
}



